package com.meida.module.bus.provider.api;

import com.meida.common.annotation.LoginRequired;
import com.meida.module.bus.client.entity.srmapi.psmtosrm.PMSResultBody;
import com.meida.module.bus.client.entity.srmapi.psmtosrm.*;
import com.meida.module.bus.provider.api.service.PmsSrmApiService;
import com.meida.module.bus.provider.api.util.ApiConstants;
import com.meida.module.bus.provider.api.util.PmsApiUtil;
import com.meida.module.bus.provider.api.util.PmsLogUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 控制器
 *
 * @author ldc
 * @date 2020-09-26
 */
@Slf4j
@Api(tags = "PMS-SRM对外回调接口服務")
@RestController
@RequestMapping("/SrmApi")
public class PmsSrmApi {

    @Autowired
    private PmsSrmApiService pmsSrmApiService;
    @Autowired
    private PmsLogUtil pmsLogUtil;

    @ApiOperation(value = "需求申请明细关闭接口")
    @RequestMapping(value = "/pms01", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @LoginRequired(required = false)
    public PMSResultBody pms01(@Valid @RequestBody PmsSrm01 pmsSrm01) {
        try {
            // 请求记录
            pmsLogUtil.saveBusSrmApiLog(ApiConstants.PMS01_LOG_MODEL_REQ, pmsSrm01.toString());
            pmsSrmApiService.pms01(pmsSrm01);
            return PMSResultBody.ok("处理正常");

        } catch (Exception ex) {
            ex.printStackTrace();
            pmsLogUtil.saveBusSrmApiExpLog(ApiConstants.PMS01_LOG_MODEL_RES, ex.getMessage());
            return PMSResultBody.err(ex.getMessage());
        }

    }

    @ApiOperation(value = "采购申请信息同步接口")
    @RequestMapping(value = "/pms02", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @LoginRequired(required = false)
    public PMSResultBody pms02(@RequestBody PmsSrm02 pmsSrm02) {
        try {
            // 请求记录
            pmsLogUtil.saveBusSrmApiLog(ApiConstants.PMS02_LOG_MODEL_REQ, pmsSrm02.toString());
            pmsSrmApiService.pms02(pmsSrm02);
            return PMSResultBody.ok("处理正常");
        } catch (Exception ex) {
            ex.printStackTrace();
            pmsLogUtil.saveBusSrmApiExpLog(ApiConstants.PMS02_LOG_MODEL_RES, ex.getMessage());
            return PMSResultBody.err(ex.getMessage());
        }
    }

    @ApiOperation(value = "开口订单转单通知")
    @RequestMapping(value = "/pms03", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @LoginRequired(required = false)
    public PMSResultBody pms03(@RequestBody PmsSrm03 pmsSrm03) {
        try {
            // 请求记录
            pmsLogUtil.saveBusSrmApiLog(ApiConstants.PMS03_LOG_MODEL_REQ, pmsSrm03.toString());
            pmsSrmApiService.pms03(pmsSrm03);
            return PMSResultBody.ok("处理正常");
        } catch (Exception ex) {
            ex.printStackTrace();
            pmsLogUtil.saveBusSrmApiExpLog(ApiConstants.PMS03_LOG_MODEL_RES, ex.getMessage());
            return PMSResultBody.err(ex.getMessage());
        }
    }

    @ApiOperation(value = "订单信息同步")
    @RequestMapping(value = "/pms04", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @LoginRequired(required = false)
    public PMSResultBody pms04(@RequestBody PmsSrm04 pmsSrm04) {
        try {
            // 请求记录
            pmsLogUtil.saveBusSrmApiLog(ApiConstants.PMS04_LOG_MODEL_REQ, pmsSrm04.toString());
            pmsSrmApiService.pms04(pmsSrm04);
            return PMSResultBody.ok("处理正常");
        } catch (Exception ex) {
            ex.printStackTrace();
            pmsLogUtil.saveBusSrmApiExpLog(ApiConstants.PMS04_LOG_MODEL_RES, ex.getMessage());
            return PMSResultBody.err(ex.getMessage());
        }
    }

    @ApiOperation(value = "收货【含冲销】信息同步")
    @RequestMapping(value = "/pms05", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @LoginRequired(required = false)
    public PMSResultBody pms05(@RequestBody PmsSrm05 pmsSrm05) {
        try {
            // 请求记录
            pmsLogUtil.saveBusSrmApiLog(ApiConstants.PMS05_LOG_MODEL_REQ, pmsSrm05.toString());
            pmsSrmApiService.pms05(pmsSrm05);
            return PMSResultBody.ok("处理正常");

        } catch (Exception ex) {
            ex.printStackTrace();
            pmsLogUtil.saveBusSrmApiExpLog(ApiConstants.PMS05_LOG_MODEL_RES, ex.getMessage());
            return PMSResultBody.err(ex.getMessage());

        }
    }

    @ApiOperation(value = "超收报警")
    @RequestMapping(value = "/pms06", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @LoginRequired(required = false)
    public PMSResultBody pms06(@RequestBody PmsSrm06 pmsSrm06) {
        try {
            // 请求记录
            pmsLogUtil.saveBusSrmApiLog(ApiConstants.PMS06_LOG_MODEL_REQ, pmsSrm06.toString());
            pmsSrmApiService.pms06(pmsSrm06);
            return PMSResultBody.ok("处理正常");

        } catch (Exception ex) {
            ex.printStackTrace();
            pmsLogUtil.saveBusSrmApiExpLog(ApiConstants.PMS06_LOG_MODEL_RES, ex.getMessage());
            return PMSResultBody.err(ex.getMessage());

        }

    }

    @ApiOperation(value = "通知错误回调通知")
    @RequestMapping(value = "/pms07", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @LoginRequired(required = false)
    public PMSResultBody pms07(@RequestBody PmsSrm07 pmsSrm07) {
        try {
            // 请求记录
            pmsLogUtil.saveBusSrmApiLog(ApiConstants.PMS07_LOG_MODEL_REQ, pmsSrm07.toString());
            pmsSrmApiService.pms07(pmsSrm07);
            return PMSResultBody.ok("处理正常");

        } catch (Exception ex) {
            ex.printStackTrace();
            pmsLogUtil.saveBusSrmApiExpLog(ApiConstants.PMS07_LOG_MODEL_RES, ex.getMessage());
            return PMSResultBody.err(ex.getMessage());

        }
    }

    @ApiOperation(value = "分包合同驳回反冲同步")
    @RequestMapping(value = "/pms08", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @LoginRequired(required = false)
    public PMSResultBody pms08(@RequestBody PmsSrm08 pmsSrm08) {
        try {
            // 请求记录
            pmsLogUtil.saveBusSrmApiLog(ApiConstants.PMS08_LOG_MODEL_REQ, pmsSrm08.toString());
            return pmsSrmApiService.pms08(pmsSrm08);
        } catch (Exception ex) {
            ex.printStackTrace();
            pmsLogUtil.saveBusSrmApiExpLog(ApiConstants.PMS08_LOG_MODEL_RES, ex.getMessage());
            return PMSResultBody.err(ex.getMessage());
        }
    }

}
